
#include "widget.h"
#include "./ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //打开谱面文件
    mMS = new MusicScore(this);
    QString fileName = "D:/CppHomework/MusicScore/WuRenQu.txt";//先只打开这一个文件就好了
    mMS->InitByFile(fileName);

    this->setFixedSize(800,600);

    //初始化主界面和场景
    mGameView.setSceneRect(QRect(0,0,800,600));//游戏主界面大小
    mScene.setSceneRect(QRect(0,0,800,600));//场景大小
    mScene.setBackgroundBrush (QBrush(qRgb (0,0,0)));//场景颜色
    mStartScene.setSceneRect(QRect(0,0,800,600));//场景大小
    mStartScene.setBackgroundBrush (QBrush(qRgb (0,0,0)));//场景颜色

    //-------------------开始场景------------------------------
    //给背景和轨道设置图片
    mTitle1.setPixmap(QPixmap(":/img/Title1.png"));
    mTitle2.setPixmap(QPixmap(":/img/Title2.png"));
    auto mTitle3 = new QPushButton();
    mTitle3->resize(498,75);
    mTitle3->setIcon(QIcon(":/img/Title4.png"));
    mTitle3->setIconSize(QSize(498,75));

    //gif1.setSize(QSize(256,103));
    // player.setVideoOutput(&gif1);//添加显示器
    // player.setSource(QUrl(":/gif/xinhaichigua.gif"));//设置播放视频
    //  player.play();

    //谱面计时器实现
    mMSTimer = new QTimer(this);
    mDelay = new QTimer(this);
    mDetectKeyTimer = new QTimer(this);
    mBorder = new QTimer(this);

    //按钮跳转事件和计时器打开事件
    connect(mTitle3,&QToolButton::clicked,[this]{
        mGameView.setScene(&mScene);
        mGameView.show();
        mDelay->start(3000);//设置3秒缓冲
    });

    connect(mDelay,&QTimer::timeout,[this](){
        mMSTimer->start(10);//打开谱面计时器
        mDetectKeyTimer->start(5);//打开判定检测器
        mBorder->start(5);
        mDelay->stop();
    });

    connect(mMSTimer,&QTimer::timeout,[this](){
        mMS->notesMove(mMS->speed);
    });

    connect(mDetectKeyTimer,&QTimer::timeout,this,&Widget::judgeStatus);
    connect(mDetectKeyTimer,&QTimer::timeout,this,&Widget::ShowStatus);
    connect(mBorder,&QTimer::timeout,[this](){
        mMS->JudgeBorder(&mScene);
    });


    //初始化各个元素的位置
    mBackground.setPos(0,0);
    mTitle1.setPos(237,50);
    mTitle2.setPos(134,259);
    mTitle3->move(151,380);
    // player.setPosition(qint64());

    mStartScene.addItem(&mStartBackground);
    mStartScene.addItem(&mTitle1);
    mStartScene.addItem(&mTitle2);
    mStartScene.addWidget(mTitle3);
    // mStartScene.addItem(&gif1);
    //--------------------主场景-------------------------------

    //给背景和轨道设置图片
    mTrack1.setPixmap(QPixmap(":/img/TrackC75.png"));
    mTrack2.setPixmap(QPixmap(":/img/TrackC75.png"));
    mTrack3.setPixmap(QPixmap(":/img/TrackC75.png"));
    mTrack4.setPixmap(QPixmap(":/img/TrackC75.png"));
    mBackground.setPixmap(QPixmap(mMS->BackGround));//临时，到时候做接口
    QGraphicsBlurEffect *e0 = new QGraphicsBlurEffect(this);//模糊效果
    e0->setBlurRadius(30);
    mBackground.setGraphicsEffect(e0);
    QGraphicsOpacityEffect *opacityEffect = new QGraphicsOpacityEffect;//透明效果
    opacityEffect->setOpacity(0.7);
    mBackground.setGraphicsEffect(opacityEffect);

    //初始化分数
    mScore = new QGraphicsTextItem;
    auto score = QString("%1").arg(Score, 7, 10, QLatin1Char('0'));
    mScore->setPlainText(score);
    mScore->setDefaultTextColor(QColor(255,255,255));//颜色
    mScore->setFont(QFont("Courier New",23,100));//字体

    //初始化难度
    mLv = new QGraphicsTextItem;
    auto lv = QString("Lv.%1").arg(mMS->level);
    mLv->setPlainText(lv);
    mLv->setDefaultTextColor(QColor(255,255,255));//颜色
    mLv->setFont(QFont("Courier New",20,60));//字体

    //初始化谱面名称
    mPName = new QGraphicsTextItem;
    mPName->setPlainText(mMS->name);
    mPName->setDefaultTextColor(QColor(255,255,255));//颜色
    mPName->setFont(QFont("Courier New",14,80));//字体

    //初始化combo标签
    mCombo->setDefaultTextColor(QColor(255,255,255));//颜色
    mCombo->setFont(QFont("Dosis",50,80));//字体

    //初始化连击数
    mComboNum = new QGraphicsTextItem;
    auto Combonum = QString("%1").arg(ComboNum);//临时连击数字符串（到时候会封装在谱面类中）
    mComboNum->setPlainText(Combonum);
    mComboNum->setDefaultTextColor(QColor(255,255,255));//颜色
    mComboNum->setFont(QFont("Dosis",45,80));//字体

    //初始化各个元素的位置
    mBackground.setPos(0,0);
    mTrack1.setPos(300,0);//各轨道位置
    mTrack2.setPos(380,0);
    mTrack3.setPos(460,0);
    mTrack4.setPos(540,0);
    mScore->setPos(640,0);//分数位置
    mLv->setPos(700,565);//难度位置
    mPName->setPos(5,565);//谱面名称位置
    mCombo->setPos(60,150);

    qreal width1 = mCombo->boundingRect().width();//"Combo"宽度
    qreal width2 = mComboNum->boundingRect().width();//ComboNum宽度
    mComboNum->setPos(60+width1/2-width2/2,230);//初始化ComboNum位置(居中)

    mStatus = new QGraphicsTextItem;//状态显示
    mStatus->setFont(QFont("Dosis",30,80));//字体


    mScene.addItem(&mBackground);//设置背景图到场景
    mScene.addItem(&mTrack1);//设置轨道到场景
    mScene.addItem(&mTrack2);
    mScene.addItem(&mTrack3);
    mScene.addItem(&mTrack4);
    mScene.addItem(mScore);//设置分数标签到场景
    mScene.addItem(mLv);//设置难度标签到场景
    mScene.addItem(mPName);//设置谱面名称标签到场景
    mScene.addItem(mCombo);//设置Combo标签到场景
    mScene.addItem(mComboNum);//设置连击数标签到场景
    mMS->setparent(&mScene);//添加谱面至场景
    mScene.addItem(mStatus);//设置状态显示到场景


    //添加场景到视图
    mGameView.setScene(&mStartScene);
    mGameView.setParent(this);//设置视图的父类
    mGameView.show();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::judgeStatus()
{
    for(int keyCode : mKeyList){
        switch(keyCode){
        case Qt::Key_S:
        {
            mMS->status = mMS->JudgeStatus(1,&mScene);//按键S->1轨道
            mKeyList.removeOne(Qt::Key_S);
            break;
        }
        case Qt::Key_D:
        {
            mMS->status = mMS->JudgeStatus(2,&mScene);//按键D->2轨道
            mKeyList.removeOne(Qt::Key_D);
            break;
        }
        case Qt::Key_J:
        {
            mMS->status = mMS->JudgeStatus(3,&mScene);//按键J->3轨道
            mKeyList.removeOne(Qt::Key_J);
            break;
        }
        case Qt::Key_K:
        {
            mMS->status = mMS->JudgeStatus(4,&mScene);//按键K->4轨道
            mKeyList.removeOne(Qt::Key_K);
            break;
        }
        }
    }
}

void Widget::ShowStatus()
{
    QString Status;
    switch(mMS->status){
    case 0:
    {
        Status = " ";
        mStatus->setPos(80,300);//状态显示位置
        break;
    }
    case 1:
    {
        Status = "Perfect";
        mStatus->setDefaultTextColor(QColor(255,222,3));//颜色
        mStatus->setPos(80,300);//状态显示位置
        break;
    }
    case 2:
    {
        Status = "Good";
        mStatus->setDefaultTextColor(QColor(160,255,255));//颜色
        mStatus->setPos(105,300);//状态显示位置
        break;
    }
    case 3:
    {
        Status = "Bad";
        mStatus->setDefaultTextColor(QColor(255,255,255));//颜色
        mStatus->setPos(117,300);//状态显示位置
        break;
    }
    case 4:
    {
        Status = "Miss";
        mStatus->setDefaultTextColor(QColor(255,255,255));//颜色
        mStatus->setPos(108,300);//状态显示位置
        break;
    }
    }
    mStatus->setPlainText(Status);
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    //添加对应按键组合
    switch(event->key()){
    case Qt::Key_S:
    case Qt::Key_D:
    case Qt::Key_J:
    case Qt::Key_K:
        mKeyList.append(event->key());
        break;
    }
}

void Widget::keyReleaseEvent(QKeyEvent *event)
{
    //移除对应按键组合
    if(mKeyList.contains(event->key())){
        mKeyList.removeOne(event->key());
    }
}


